<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Symfony\Component\HttpFoundation\File\UploadedFile ;

class UploadController extends Controller
{
    public function image(Request $request)
    {

        $file = $request->file("img_name");
        foreach ($file as $key => $value) {
            if(!empty($value)){//此处防止没有多文件上传的情况
                $allowed_extensions = ["png", "jpg", "gif"];
                if ($value->getClientOriginalExtension() && !in_array($value->getClientOriginalExtension(), $allowed_extensions)) {
                    return ['error' => 'You may only upload png, jpg or gif.'];die;
                }
                $destinationPath = 'storage/uploads/'; //public 文件夹下面建 storage/uploads 文件夹
                $extension = $value->getClientOriginalExtension();
                $fileName = str_random(10).'.'.$extension;//重命名
                $value->move($destinationPath, $fileName);
                $filePath = asset($destinationPath.$fileName);
                $post['landlord_img']="storage/uploads/".$fileName;
                $list=array('img_name'=>$fileName,'house_id'=>$id);
                DB::table('img')->insert($list);
            }
        }

    }
    public function upload_image(Request $request)
    {
        if ($request->isMethod('POST')) {
//            var_dump($_FILES);
            $file = $request->file('source');

            //判断文件是否上传成功
            if ($file->isValid()) {
                //获取原文件名
                $originalName = $file->getClientOriginalName();
                //扩展名
                $ext = $file->getClientOriginalExtension();
                //文件类型
                $type = $file->getClientMimeType();
                //临时绝对路径
                $realPath = $file->getRealPath();

                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '-' . $ext;

                $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));

                var_dump($bool);
            }
        }
    }



























}
